# -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core import urlresolvers
from django.contrib.auth.models import User
from django.db.models import Max


class Game(models.Model):

    CH_STATUS = (
        ('PN', u'Planned'),
        ('PY', u'Playing'),
        ('FN', u'Finished'),
    )

    status = models.CharField(verbose_name=_(u'Status game'), max_length=3, blank=False, null=False, choices=CH_STATUS)

    #num_players permitidos
    
    delay = models.FloatField(verbose_name=_(u'Max players delay in game'), blank=True, null=True, default=0.0)

    date_created = models.DateTimeField(null=True, auto_now_add=True)

    date_updated = models.DateTimeField(null=True, auto_now=True)

    #date_planned = models.DateTimeField(null=False)


    class Meta:
        app_label = 'game'
        verbose_name = _(u'Game')
        verbose_name_plural = _(u'Game')


    def __unicode__(self):
        return unicode(self.id) + u' -- ' + unicode(self.get_status_display()) + u' -- ' + unicode(self.date_created)

    # public
    def is_game_over(self):
        if self.players.filter(is_dead = False).count() == 1:
            return True
        else:
            return False

    def update_delay(self):
        self.delay = self.players.aggregate(Max('delay'))['delay__max']
        self.save()

    # public
    def game_over(self):
        self.status = 'FN'
        self.save()

        """
        player = self.players.get(is_dead = False)
        statistic = player.user_profile.statistics.get(status='P')
        statistic.games += 1
        statistic.games_win += 1
        """

        """
        team = self.teams.get(is_dead = False)
        for player in team.players:
            statistic = player.user_profile.statistics.get(status='P')
            statistic.games += 1
            statistic.games_win += 1
        """

    """
    def save()
    """
